<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="辅助函数" tabindex="-1"><a class="header-anchor" href="#辅助函数"><span>辅助函数</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#available-methods">可用方法</a></li>
<li><a href="#other-utilities">其它实用工具</a>
<ul>
<li><a href="#benchmarking">基准测试</a></li>
<li><a href="#dates">日期</a></li>
<li><a href="#lottery">抽奖</a></li>
<li><a href="#pipeline">管道</a></li>
<li><a href="#sleep">睡眠</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 包含了多种全局的PHP 助手函数。这些函数许多被框架本身使用；然而，如果你觉得它们方便，你也可以在你自己的应用程序中自由使用它们。</p>
<h2 id="可用方法" tabindex="-1"><a class="header-anchor" href="#可用方法"><span>可用方法</span></a></h2>
<h3 id="数组-对象" tabindex="-1"><a class="header-anchor" href="#数组-对象"><span>数组 &amp; 对象</span></a></h3>
<h3 id="数字" tabindex="-1"><a class="header-anchor" href="#数字"><span>数字</span></a></h3>
<h3 id="路径" tabindex="-1"><a class="header-anchor" href="#路径"><span>路径</span></a></h3>
<h3 id="urls" tabindex="-1"><a class="header-anchor" href="#urls"><span>URLs</span></a></h3>
<h3 id="杂项" tabindex="-1"><a class="header-anchor" href="#杂项"><span>杂项</span></a></h3>
<h2 id="数组-对象-1" tabindex="-1"><a class="header-anchor" href="#数组-对象-1"><span>数组 &amp; 对象</span></a></h2>
<h4 id="arr-accessible" tabindex="-1"><a class="header-anchor" href="#arr-accessible"><span><code v-pre>Arr::accessible()</code></span></a></h4>
<p><code v-pre>Arr::accessible</code> 方法判断给定值是否可作为数组访问：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Collection</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAccessible</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">accessible</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'a'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'b'</span> <span class="token operator">=></span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAccessible</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">accessible</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Collection</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAccessible</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">accessible</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'abc'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAccessible</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">accessible</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">stdClass</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-add" tabindex="-1"><a class="header-anchor" href="#arr-add"><span><code v-pre>Arr::add()</code></span></a></h4>
<p>如果给定的键名在数组中不存在键值或该键值设置为 <code v-pre>null</code> ，那么 <code v-pre>Arr::add</code> 方法将会把给定的键值对添加到数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk', 'price' => 100]</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token constant">null</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk', 'price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-collapse" tabindex="-1"><a class="header-anchor" href="#arr-collapse"><span><code v-pre>Arr::collapse()</code></span></a></h4>
<p><code v-pre>Arr::collapse</code> 方法将多个数组合并为一个数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">collapse</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [1, 2, 3, 4, 5, 6, 7, 8, 9]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-crossjoin" tabindex="-1"><a class="header-anchor" href="#arr-crossjoin"><span><code v-pre>Arr::crossJoin()</code></span></a></h4>
<p><code v-pre>Arr::crossJoin</code> 方法交叉连接给定的数组，返回具有所有可能排列的笛卡尔乘积：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$matrix</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">crossJoin</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'b'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        [1, 'a'],</span>
<span class="line">        [1, 'b'],</span>
<span class="line">        [2, 'a'],</span>
<span class="line">        [2, 'b'],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$matrix</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">crossJoin</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'b'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'I'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'II'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        [1, 'a', 'I'],</span>
<span class="line">        [1, 'a', 'II'],</span>
<span class="line">        [1, 'b', 'I'],</span>
<span class="line">        [1, 'b', 'II'],</span>
<span class="line">        [2, 'a', 'I'],</span>
<span class="line">        [2, 'a', 'II'],</span>
<span class="line">        [2, 'b', 'I'],</span>
<span class="line">        [2, 'b', 'II'],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-divide" tabindex="-1"><a class="header-anchor" href="#arr-divide"><span><code v-pre>Arr::divide()</code></span></a></h4>
<p><code v-pre>Arr::divide</code> 方法返回一个二维数组，一个值包含原数组的键，另一个值包含原数组的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">[</span><span class="token variable">$keys</span><span class="token punctuation">,</span> <span class="token variable">$values</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">divide</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $keys: ['name']</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $values: ['Desk']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-dot" tabindex="-1"><a class="header-anchor" href="#arr-dot"><span><code v-pre>Arr::dot()</code></span></a></h4>
<p><code v-pre>Arr::dot</code> 方法将多维数组中所有的键平铺到一维数组中，新数组使用「.」符号表示层级包含关系：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$flattened</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">dot</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products.desk.price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-except" tabindex="-1"><a class="header-anchor" href="#arr-except"><span><code v-pre>Arr::except()</code></span></a></h4>
<p><code v-pre>Arr::except</code> 方法从数组中删除指定的键值对：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$filtered</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">except</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-exists" tabindex="-1"><a class="header-anchor" href="#arr-exists"><span><code v-pre>Arr::exists()</code></span></a></h4>
<p><code v-pre>Arr::exists</code> 方法检查给定的键是否存在提供的数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'John Doe'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'age'</span> <span class="token operator">=></span> <span class="token number">17</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$exists</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$exists</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'salary'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-first" tabindex="-1"><a class="header-anchor" href="#arr-first"><span><code v-pre>Arr::first()</code></span></a></h4>
<p><code v-pre>Arr::first</code> 方法返回数组中满足指定条件的第一个元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$first</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">first</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword type-hint">int</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">>=</span> <span class="token number">150</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 200</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可将默认值作为第三个参数传递给该方法，如果数组中没有值满足指定条件，则返回该默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$first</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">first</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-flatten" tabindex="-1"><a class="header-anchor" href="#arr-flatten"><span><code v-pre>Arr::flatten()</code></span></a></h4>
<p><code v-pre>Arr::flatten</code> 方法将多维数组中数组的值取出平铺为一维数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Joe'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'languages'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'PHP'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Ruby'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$flattened</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">flatten</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Joe', 'PHP', 'Ruby']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-forget" tabindex="-1"><a class="header-anchor" href="#arr-forget"><span><code v-pre>Arr::forget()</code></span></a></h4>
<p><code v-pre>Arr::forget</code> 方法使用「.」符号从深度嵌套的数组中删除给定的键值对：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">forget</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => []]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-get" tabindex="-1"><a class="header-anchor" href="#arr-get"><span><code v-pre>Arr::get()</code></span></a></h4>
<p><code v-pre>Arr::get</code> 方法使用「.」符号从深度嵌套的数组中根据指定键检索值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$price</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 100</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>Arr::get</code> 方法也可以接受一个默认值，如果数组中不存在指定的键，则返回默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$discount</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.discount'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 0</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-has" tabindex="-1"><a class="header-anchor" href="#arr-has"><span><code v-pre>Arr::has()</code></span></a></h4>
<p><code v-pre>Arr::has</code> 方法使用「.」符号判断数组中是否存在指定的一个或多个键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'product'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">has</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">has</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'product.price'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product.discount'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-hasany" tabindex="-1"><a class="header-anchor" href="#arr-hasany"><span><code v-pre>Arr::hasAny()</code></span></a></h4>
<p><code v-pre>Arr::hasAny</code> 方法使用「.」符号判断给定集合中的任一值是否存在于数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'product'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">hasAny</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">hasAny</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'product.name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product.discount'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">hasAny</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'category'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product.discount'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-isassoc" tabindex="-1"><a class="header-anchor" href="#arr-isassoc"><span><code v-pre>Arr::isAssoc()</code></span></a></h4>
<p>如果给定数组是关联数组，则 <code v-pre>Arr::isAssoc</code> 方法返回 <code v-pre>true</code>，如果该数组没有以零开头的顺序数字键，则将其视为「关联」数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAssoc</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">isAssoc</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'product'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAssoc</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">isAssoc</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-islist" tabindex="-1"><a class="header-anchor" href="#arr-islist"><span><code v-pre>Arr::isList()</code></span></a></h4>
<p>如果给定数组的键是从零开始的连续整数，则 <code v-pre>Arr::isList</code> 方法返回 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isList</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">isList</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'baz'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isList</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">isList</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'product'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-join" tabindex="-1"><a class="header-anchor" href="#arr-join"><span><code v-pre>Arr::join()</code></span></a></h4>
<p><code v-pre>Arr::join()</code> 方法将给定数组的所有值通过给定字符串连接起来。使用此方法的第二个参数，你还可以为数组中的最后一个元素指定连接的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Tailwind'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Alpine'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Livewire'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$joined</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">', '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Tailwind, Alpine, Laravel, Livewire</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$joined</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">', '</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">' and '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Tailwind, Alpine, Laravel and Livewire</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-keyby" tabindex="-1"><a class="header-anchor" href="#arr-keyby"><span><code v-pre>Arr::keyBy()</code></span></a></h4>
<p><code v-pre>Arr::keyBy()</code> 方法通过给定键名的值对该数组进行重组。如果数组中存在多个相同的值，则只有最后一个值会出现在新数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'product_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'prod-100'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'product_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'prod-200'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$keyed</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">keyBy</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],</span>
<span class="line">        'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-last" tabindex="-1"><a class="header-anchor" href="#arr-last"><span><code v-pre>Arr::last()</code></span></a></h4>
<p><code v-pre>Arr::last</code> 方法返回数组中满足指定条件的最后一个元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">110</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$last</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">last</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword type-hint">int</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">>=</span> <span class="token number">150</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 300</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>将默认值作为第三个参数传递给该方法，如果没有值满足条件，则返回该默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$last</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">last</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-map" tabindex="-1"><a class="header-anchor" href="#arr-map"><span><code v-pre>Arr::map()</code></span></a></h4>
<p><code v-pre>Arr::map</code> 方法用来遍历数组，并将每个值和键传递给给定的回调。数组值由回调返回的值替换：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'first'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'james'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'last'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'kirk'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$mapped</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">ucfirst</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['first' => 'James', 'last' => 'Kirk']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-mapspread" tabindex="-1"><a class="header-anchor" href="#arr-mapspread"><span><code v-pre>Arr::mapSpread()</code></span></a></h4>
<p><code v-pre>Arr::mapSpread</code> 方法遍历数组，将每个嵌套项值传递到给定的闭包。闭包可以自由修改项并返回它，从而形成一个新的修改后的项数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$mapped</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">mapSpread</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$even</span><span class="token punctuation">,</span> <span class="token keyword type-hint">int</span> <span class="token variable">$odd</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$even</span> <span class="token operator">+</span> <span class="token variable">$odd</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [1, 5, 9, 13, 17]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-mapwithkeys" tabindex="-1"><a class="header-anchor" href="#arr-mapwithkeys"><span><code v-pre>Arr::mapWithKeys()</code></span></a></h4>
<p><code v-pre>Arr::mapWithKeys</code> 方法遍历数组并将每个值传递给给定的回调。回调应返回包含单个键值对的关联数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'John'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'department'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Sales'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'john@example.com'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Jane'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'department'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Marketing'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'jane@example.com'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$mapped</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">mapWithKeys</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$item</span><span class="token punctuation">,</span> <span class="token keyword type-hint">int</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token variable">$item</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span> <span class="token operator">=></span> <span class="token variable">$item</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        'john@example.com' => 'John',</span>
<span class="line">        'jane@example.com' => 'Jane',</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-only" tabindex="-1"><a class="header-anchor" href="#arr-only"><span><code v-pre>Arr::only()</code></span></a></h4>
<p><code v-pre>Arr::only</code> 方法仅从给定数组中返回指定的键值对：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'orders'</span> <span class="token operator">=></span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">only</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk', 'price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-pluck" tabindex="-1"><a class="header-anchor" href="#arr-pluck"><span><code v-pre>Arr::pluck()</code></span></a></h4>
<p><code v-pre>Arr::pluck</code> 方法从数组中提取给定键的所有值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'developer'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'developer'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Abigail'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$names</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'developer.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Taylor', 'Abigail']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以指定结果的键:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$names</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'developer.name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'developer.id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [1 => 'Taylor', 2 => 'Abigail']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-prepend" tabindex="-1"><a class="header-anchor" href="#arr-prepend"><span><code v-pre>Arr::prepend()</code></span></a></h4>
<p><code v-pre>Arr::prepend</code> 方法会将一个元素推到数组的开头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'one'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'two'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'three'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'four'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">prepend</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'zero'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['zero', 'one', 'two', 'three', 'four']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以指定插入值的键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">prepend</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk', 'price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-prependkeyswith" tabindex="-1"><a class="header-anchor" href="#arr-prependkeyswith"><span><code v-pre>Arr::prependKeysWith()</code></span></a></h4>
<p><code v-pre>Arr::prependKeysWith</code> 方法用给定前缀预置关联数组的所有键名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$keyed</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">prependKeysWith</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        'product.name' => 'Desk',</span>
<span class="line">        'product.price' => 100,</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-pull" tabindex="-1"><a class="header-anchor" href="#arr-pull"><span><code v-pre>Arr::pull()</code></span></a></h4>
<p><code v-pre>Arr::pull</code> 方法返回并移除数组中的一个键值对：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">pull</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $name: Desk</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $array: ['price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>也可以将默认值作为方法的第三个参数传递。如果键不存在，将返回这个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">pull</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-query" tabindex="-1"><a class="header-anchor" href="#arr-query"><span><code v-pre>Arr::query()</code></span></a></h4>
<p><code v-pre>Arr::query</code> 方法将数组转换为查询字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'order'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'column'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'direction'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'desc'</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// name=Taylor&amp;order[column]=created_at&amp;order[direction]=desc</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-random" tabindex="-1"><a class="header-anchor" href="#arr-random"><span><code v-pre>Arr::random()</code></span></a></h4>
<p><code v-pre>Arr::random</code> 方法从数组中返回一个随机值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$random</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 4 - (随机获取)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以指定作为可选第二个参数返回的项数。注意，即使只需要一个项，提供这个参数也将返回一个数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$items</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [2, 5] - (随机获取)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-set" tabindex="-1"><a class="header-anchor" href="#arr-set"><span><code v-pre>Arr::set()</code></span></a></h4>
<p><code v-pre>Arr::set</code> 方法使用「.」符号在多维数组中设置指定键的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 200]]]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-shuffle" tabindex="-1"><a class="header-anchor" href="#arr-shuffle"><span><code v-pre>Arr::shuffle()</code></span></a></h4>
<p><code v-pre>Arr::shuffle</code> 方法将数组中值进行随机排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">shuffle</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [3, 2, 5, 1, 4] - (generated randomly)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-sort" tabindex="-1"><a class="header-anchor" href="#arr-sort"><span><code v-pre>Arr::sort()</code></span></a></h4>
<p><code v-pre>Arr::sort</code> 方法根据给定数组的值进行升序排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Table'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Chair', 'Desk', 'Table']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以根据给定回调方法的返回结果对数组进行排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Table'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token function">array_values</span><span class="token punctuation">(</span><span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        ['name' => 'Chair'],</span>
<span class="line">        ['name' => 'Desk'],</span>
<span class="line">        ['name' => 'Table'],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-sortdesc" tabindex="-1"><a class="header-anchor" href="#arr-sortdesc"><span><code v-pre>Arr::sortDesc()</code></span></a></h4>
<p><code v-pre>Arr::sortDesc</code> 方法根据给定数组的值进行降序排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Table'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">sortDesc</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Table', 'Desk', 'Chair']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以根据给定回调方法的返回结果对数组进行排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Table'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token function">array_values</span><span class="token punctuation">(</span><span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">sortDesc</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        ['name' => 'Table'],</span>
<span class="line">        ['name' => 'Desk'],</span>
<span class="line">        ['name' => 'Chair'],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-sortrecursive" tabindex="-1"><a class="header-anchor" href="#arr-sortrecursive"><span><code v-pre>Arr::sortRecursive()</code></span></a></h4>
<p><code v-pre>Arr::sortRecursive</code> 方法对给定数组进行递归排序，使用 <code v-pre>sort</code> 方法对数字索引子数组进行按值升序排序，使用 <code v-pre>ksort</code> 方法对关联子数组进行按键升序排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'Roman'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Li'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'PHP'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Ruby'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'JavaScript'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'one'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'two'</span> <span class="token operator">=></span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'three'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">sortRecursive</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        ['JavaScript', 'PHP', 'Ruby'],</span>
<span class="line">        ['one' => 1, 'three' => 3, 'two' => 2],</span>
<span class="line">        ['Li', 'Roman', 'Taylor'],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你希望结果降序排序，你可以使用 <code v-pre>Arr::sortRecursiveDesc</code> 方法。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">sortRecursiveDesc</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="arr-take" tabindex="-1"><a class="header-anchor" href="#arr-take"><span><code v-pre>Arr::take()</code></span></a></h4>
<p><code v-pre>Arr::take</code> 方法返回一个新数组，其中包含指定数量的项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$chunk</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">take</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [0, 1, 2]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以传递一个负整数，以从数组的末尾取出指定数量的项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$chunk</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">take</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [4, 5]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-tocssclasses" tabindex="-1"><a class="header-anchor" href="#arr-tocssclasses"><span><code v-pre>Arr::toCssClasses()</code></span></a></h4>
<p><code v-pre>Arr::toCssClasses</code> 方法有条件地编译 CSS 类字符串。该方法接受一个键值数组，其中数组键包含你希望添加的类，而值是一个布尔表达式。如果数组元素有数字键，它将始终包括在渲染的类列表中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isActive</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$hasError</span> <span class="token operator">=</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'p-4'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'font-bold'</span> <span class="token operator">=></span> <span class="token variable">$isActive</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bg-red'</span> <span class="token operator">=></span> <span class="token variable">$hasError</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$classes</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">toCssClasses</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    'p-4 bg-red'</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-tocssstyles" tabindex="-1"><a class="header-anchor" href="#arr-tocssstyles"><span><code v-pre>Arr::toCssStyles()</code></span></a></h4>
<p><code v-pre>Arr::toCssStyles</code> 方法有条件地编译 CSS 样式字符串。该方法接受一个键值数组，其中数组键包含你希望添加的类，而值是一个布尔表达式。如果数组元素有数字键，它将始终包括在渲染的类列表中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$hasColor</span> <span class="token operator">=</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'background-color: blue'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'color: blue'</span> <span class="token operator">=></span> <span class="token variable">$hasColor</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$classes</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">toCssStyles</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    'background-color: blue; color: blue;'</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>这个方法实现了 Laravel 的功能，允许<a href="https://learnku.com/docs/laravel/11.x/blade#conditionally-merge-classes" target="_blank" rel="noopener noreferrer">与 Blade 组件的属性包合并类</a>以及 <code v-pre>@class</code> <a href="https://learnku.com/docs/laravel/11.x/blade#conditional-classes" target="_blank" rel="noopener noreferrer">Blade 指令</a>。</p>
<h4 id="arr-undot" tabindex="-1"><a class="header-anchor" href="#arr-undot"><span><code v-pre>Arr::undot()</code></span></a></h4>
<p><code v-pre>Arr::undot</code> 方法使用「.」符号将一维数组扩展为多维数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user.name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Kevin Malone'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'user.occupation'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Accountant'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">undot</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['user' => ['name' => 'Kevin Malone', 'occupation' => 'Accountant']]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-where" tabindex="-1"><a class="header-anchor" href="#arr-where"><span><code v-pre>Arr::where()</code></span></a></h4>
<p><code v-pre>Arr::where</code> 方法使用给定的回调函数返回的结果过滤数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'200'</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'400'</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$filtered</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-declaration">string</span><span class="token operator">|</span><span class="token keyword type-declaration">int</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword type-hint">int</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">is_string</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [1 => '200', 3 => '400']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-wherenotnull" tabindex="-1"><a class="header-anchor" href="#arr-wherenotnull"><span><code v-pre>Arr::whereNotNull()</code></span></a></h4>
<p><code v-pre>Arr::whereNotNull</code> 方法将从给定数组中删除所有 <code v-pre>null</code> 值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token constant">null</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$filtered</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">whereNotNull</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [0 => 0]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-wrap" tabindex="-1"><a class="header-anchor" href="#arr-wrap"><span><code v-pre>Arr::wrap()</code></span></a></h4>
<p><code v-pre>Arr::wrap</code> 方法可以将给定值转换为一个数组，如果给定的值已经是一个数组，它将原样返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">wrap</span><span class="token punctuation">(</span><span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Laravel']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果给定值是 <code v-pre>null</code> ，将返回一个空数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">wrap</span><span class="token punctuation">(</span><span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// []</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="data-fill" tabindex="-1"><a class="header-anchor" href="#data-fill"><span><code v-pre>data_fill()</code></span></a></h4>
<p><code v-pre>data_fill</code> 函数使用「.」符号给多维数组或对象设置缺少的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_fill</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 100]]]</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_fill</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.discount'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 100, 'discount' => 10]]]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>也可以接收 「*」 作为通配符，设置相应缺少的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 2'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_fill</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.*.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        'products' => [</span>
<span class="line">            ['name' => 'Desk 1', 'price' => 100],</span>
<span class="line">            ['name' => 'Desk 2', 'price' => 200],</span>
<span class="line">        ],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="data-get" tabindex="-1"><a class="header-anchor" href="#data-get"><span><code v-pre>data_get()</code></span></a></h4>
<p><code v-pre>data_get</code> 函数使用 「.」 符号从多维数组或对象中根据指定键检索值</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$price</span> <span class="token operator">=</span> <span class="token function">data_get</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 100</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>data_get</code> 函数也接受一个默认值，如果没有找到指定的键，将返回默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$discount</span> <span class="token operator">=</span> <span class="token function">data_get</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.discount'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 0</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>该函数还接受「*」作为通配符，来指向数组或对象的任何键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'product-one'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'product-two'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 2'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">150</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_get</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'*.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Desk 1', 'Desk 2'];</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可以使用 <code v-pre>{first}</code> 和 <code v-pre>{last}</code> 占位符获取数组的第一个或最后一个项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$flight</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'segments'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'from'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'LHR'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'departure'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'9:00'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'to'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'IST'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'arrival'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'15:00'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'from'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'IST'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'departure'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'16:00'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'to'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'PKX'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'arrival'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'20:00'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_get</span><span class="token punctuation">(</span><span class="token variable">$flight</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'segments.{first}.arrival'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 15:00</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="data-set" tabindex="-1"><a class="header-anchor" href="#data-set"><span><code v-pre>data_set()</code></span></a></h4>
<p><code v-pre>data_set</code> 函数使用「.」符号从多维数组或对象中根据指定键设置值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_set</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 200]]]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此函数也接受使用「*」作为通配符，并相应地在目标上设置值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 2'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">150</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_set</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.*.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        'products' => [</span>
<span class="line">            ['name' => 'Desk 1', 'price' => 200],</span>
<span class="line">            ['name' => 'Desk 2', 'price' => 200],</span>
<span class="line">        ],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认情况下，任何现有值都将被覆盖。如果你希望仅在值不存在时设置它，可以将 <code v-pre>false</code> 作为函数的第四个参数传递：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_set</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token argument-name">overwrite</span><span class="token punctuation">:</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 100]]]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="data-forget" tabindex="-1"><a class="header-anchor" href="#data-forget"><span><code v-pre>data_forget()</code></span></a></h4>
<p><code v-pre>data_forget</code> 函数使用「.」符号从嵌套数组或对象中移除值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_forget</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => []]]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此函数也接受使用「*」 作为通配符，并相应地在目标上移除值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 2'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">150</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_forget</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.*.price'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        'products' => [</span>
<span class="line">            ['name' => 'Desk 1'],</span>
<span class="line">            ['name' => 'Desk 2'],</span>
<span class="line">        ],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="head" tabindex="-1"><a class="header-anchor" href="#head"><span><code v-pre>head()</code></span></a></h4>
<p><code v-pre>head</code> 函数返回给定数组中的第一个元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$first</span> <span class="token operator">=</span> <span class="token function">head</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 100</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="last" tabindex="-1"><a class="header-anchor" href="#last"><span><code v-pre>last()</code></span></a></h4>
<p><code v-pre>last</code> 函数返回给定数组中的最后一个元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$last</span> <span class="token operator">=</span> <span class="token function">last</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 300</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="数字-1" tabindex="-1"><a class="header-anchor" href="#数字-1"><span>数字</span></a></h2>
<h4 id="number-abbreviate" tabindex="-1"><a class="header-anchor" href="#number-abbreviate"><span><code v-pre>Number::abbreviate()</code></span></a></h4>
<p><code v-pre>Number::abbreviate</code> 方法返回所提供数值的人类可读格式，并用缩写表示单位：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Number</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">abbreviate</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 1K</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">abbreviate</span><span class="token punctuation">(</span><span class="token number">489939</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 490K</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">abbreviate</span><span class="token punctuation">(</span><span class="token number">1230000</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 1.23M</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="number-clamp" tabindex="-1"><a class="header-anchor" href="#number-clamp"><span><code v-pre>Number::clamp()</code></span></a></h4>
<p><code v-pre>Number::clamp</code> 方法确保给定数字保持在指定范围内。如果数字低于最小值，则返回最小值。如果数字高于最大值，则返回最大值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Number</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">clamp</span><span class="token punctuation">(</span><span class="token number">105</span><span class="token punctuation">,</span> <span class="token argument-name">min</span><span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token argument-name">max</span><span class="token punctuation">:</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 100</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">clamp</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token argument-name">min</span><span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token argument-name">max</span><span class="token punctuation">:</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 10</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">clamp</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token argument-name">min</span><span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token argument-name">max</span><span class="token punctuation">:</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 10</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">clamp</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token argument-name">min</span><span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token argument-name">max</span><span class="token punctuation">:</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 20</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="number-currency" tabindex="-1"><a class="header-anchor" href="#number-currency"><span><code v-pre>Number::currency()</code></span></a></h4>
<p><code v-pre>Number::currency</code> 方法将给定值作为字符串返回货币表示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Number</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$currency</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">currency</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $1,000</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$currency</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">currency</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">,</span> <span class="token argument-name">in</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'EUR'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// €1,000</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$currency</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">currency</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">,</span> <span class="token argument-name">in</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'EUR'</span><span class="token punctuation">,</span> <span class="token argument-name">locale</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'de'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 1.000 €</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="number-filesize" tabindex="-1"><a class="header-anchor" href="#number-filesize"><span><code v-pre>Number::fileSize()</code></span></a></h4>
<p><code v-pre>Number::fileSize</code> 方法将给定字节值作为字符串返回文件大小表示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Number</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$size</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">fileSize</span><span class="token punctuation">(</span><span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 1 KB</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$size</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">fileSize</span><span class="token punctuation">(</span><span class="token number">1024</span> <span class="token operator">*</span> <span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 1 MB</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$size</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">fileSize</span><span class="token punctuation">(</span><span class="token number">1024</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 1.00 KB</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="number-forhumans" tabindex="-1"><a class="header-anchor" href="#number-forhumans"><span><code v-pre>Number::forHumans()</code></span></a></h4>
<p><code v-pre>Number::forHumans</code> 方法返回所提供数值的人类可读格式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Number</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">forHumans</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 1 thousand</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">forHumans</span><span class="token punctuation">(</span><span class="token number">489939</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 490 thousand</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">forHumans</span><span class="token punctuation">(</span><span class="token number">1230000</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 1.23 million</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="number-format" tabindex="-1"><a class="header-anchor" href="#number-format"><span><code v-pre>Number::format()</code></span></a></h4>
<p><code v-pre>Number::format</code> 方法将给定数字格式化为特定区域的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Number</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token number">100000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 100,000</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token number">100000</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 100,000.00</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token number">100000.123</span><span class="token punctuation">,</span> <span class="token argument-name">maxPrecision</span><span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 100,000.12</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token number">100000</span><span class="token punctuation">,</span> <span class="token argument-name">locale</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'de'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 100.000</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="number-ordinal" tabindex="-1"><a class="header-anchor" href="#number-ordinal"><span><code v-pre>Number::ordinal()</code></span></a></h4>
<p><code v-pre>Number::ordinal</code> 方法返回数字的序数表示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Number</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">ordinal</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 1st</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">ordinal</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 2nd</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">ordinal</span><span class="token punctuation">(</span><span class="token number">21</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 21st</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="number-percentage" tabindex="-1"><a class="header-anchor" href="#number-percentage"><span><code v-pre>Number::percentage()</code></span></a></h4>
<p><code v-pre>Number::percentage</code> 方法将给定值作为字符串返回百分比表示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Number</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$percentage</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">percentage</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 10%</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$percentage</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">percentage</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 10.00%</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$percentage</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">percentage</span><span class="token punctuation">(</span><span class="token number">10.123</span><span class="token punctuation">,</span> <span class="token argument-name">maxPrecision</span><span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 10.12%</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$percentage</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">percentage</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token argument-name">locale</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'de'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 10,00%</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="number-spell" tabindex="-1"><a class="header-anchor" href="#number-spell"><span><code v-pre>Number::spell()</code></span></a></h4>
<p><code v-pre>Number::spell</code> 方法将给定数字转换为字词字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Number</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">spell</span><span class="token punctuation">(</span><span class="token number">102</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// one hundred and two</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">spell</span><span class="token punctuation">(</span><span class="token number">88</span><span class="token punctuation">,</span> <span class="token argument-name">locale</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'fr'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// quatre-vingt-huit</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>after</code> 参数允许你指定在哪个值之后所有数字都应该拼写出来：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">spell</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token argument-name">after</span><span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 10</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">spell</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">,</span> <span class="token argument-name">after</span><span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// eleven</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>until</code> 参数允许你指定在哪个值之前所有数字都应该拼写出来：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">spell</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token argument-name">until</span><span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// five</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">spell</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token argument-name">until</span><span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 10</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="number-uselocale" tabindex="-1"><a class="header-anchor" href="#number-uselocale"><span><code v-pre>Number::useLocale()</code></span></a></h4>
<p><code v-pre>Number::useLocale</code> 方法全局设置默认数字区域设置，这影响了 <code v-pre>Number</code> 类方法后续调用时数字和货币的格式化方式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Number</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 启动任何应用服务</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">useLocale</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'de'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="number-withlocale" tabindex="-1"><a class="header-anchor" href="#number-withlocale"><span><code v-pre>Number::withLocale()</code></span></a></h4>
<p><code v-pre>Number::withLocale</code> 方法在指定区域设置中执行给定闭包，然后在回调执行后恢复原始区域设置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Number</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$number</span> <span class="token operator">=</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">withLocale</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'de'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">Number</span><span class="token operator">::</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token number">1500</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="路径-1" tabindex="-1"><a class="header-anchor" href="#路径-1"><span>路径</span></a></h2>
<h4 id="app-path" tabindex="-1"><a class="header-anchor" href="#app-path"><span><code v-pre>app_path()</code></span></a></h4>
<p><code v-pre>app_path</code> 函数返回应用程序的 <code v-pre>app</code> 目录的完整路径。你也可以使用 <code v-pre>app_path</code> 函数生成相对于应用程序目录的文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">app_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">app_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Http/Controllers/Controller.php'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="base-path" tabindex="-1"><a class="header-anchor" href="#base-path"><span><code v-pre>base_path()</code></span></a></h4>
<p><code v-pre>base_path</code> 函数返回应用程序的根目录的完整路径。你还可以使用 <code v-pre>base_path</code> 函数生成相对于项目根目录的给定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">base_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">base_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'vendor/bin'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="config-path" tabindex="-1"><a class="header-anchor" href="#config-path"><span><code v-pre>config_path()</code></span></a></h4>
<p><code v-pre>config_path</code> 函数返回应用程序的 <code v-pre>config</code> 目录的完整路径。你也可以使用 <code v-pre>config_path</code> 函数生成应用程序配置目录中给定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">config_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">config_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.php'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="database-path" tabindex="-1"><a class="header-anchor" href="#database-path"><span><code v-pre>database_path()</code></span></a></h4>
<p><code v-pre>database_path</code> 函数返回应用程序的 <code v-pre>database</code> 目录的完整路径。你还可以使用 <code v-pre>database_path</code> 函数生成数据库目录中给定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">database_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">database_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'factories/UserFactory.php'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="lang-path" tabindex="-1"><a class="header-anchor" href="#lang-path"><span><code v-pre>lang_path()</code></span></a></h4>
<p><code v-pre>lang_path</code> 函数返回应用程序的 <code v-pre>lang</code> 目录的完整路径。你也可以使用 <code v-pre>lang_path</code> 函数生成目录中给定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">lang_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">lang_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'en/messages.php'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
默认情况下，Laravel 应用程序骨架不包括 <code v-pre>lang</code> 目录。如果你希望自定义 Laravel 的语言文件，则可以通过 <code v-pre>lang:publish</code> Artisan 命令发布它们。</p>
</blockquote>
<h4 id="mix" tabindex="-1"><a class="header-anchor" href="#mix"><span><code v-pre>mix()</code></span></a></h4>
<p><code v-pre>mix</code> 函数返回到 <a href="https://learnku.com/docs/laravel/11.x/mix" target="_blank" rel="noopener noreferrer">版本化的 Mix 文件</a> 的路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">mix</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'css/app.css'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="public-path" tabindex="-1"><a class="header-anchor" href="#public-path"><span><code v-pre>public_path()</code></span></a></h4>
<p><code v-pre>public_path</code> 函数返回应用程序的 <code v-pre>public</code> 目录的完全限定路径。你还可以使用 <code v-pre>public_path</code> 函数生成公共目录中给定文件的完全限定路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">public_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">public_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'css/app.css'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="resource-path" tabindex="-1"><a class="header-anchor" href="#resource-path"><span><code v-pre>resource_path()</code></span></a></h4>
<p><code v-pre>resource_path</code> 函数返回应用程序的 <code v-pre>resources</code> 目录的完全限定路径。你还可以使用 <code v-pre>resource_path</code> 函数生成资源目录中给定文件的完全限定路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sass/app.scss'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="storage-path" tabindex="-1"><a class="header-anchor" href="#storage-path"><span><code v-pre>storage_path()</code></span></a></h4>
<p><code v-pre>storage_path</code> 函数返回应用程序的 <code v-pre>storage</code> 目录的完全限定路径。你还可以使用 <code v-pre>storage_path</code> 函数生成存储目录中给定文件的完全限定路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app/file.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="urls-1" tabindex="-1"><a class="header-anchor" href="#urls-1"><span>URLs</span></a></h2>
<h4 id="action" tabindex="-1"><a class="header-anchor" href="#action"><span><code v-pre>action()</code></span></a></h4>
<p><code v-pre>action</code> 函数为给定的控制器操作生成 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>HomeController</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">action</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token class-name static-context">HomeController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'index'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果该方法接受路由参数，则可以将它们作为第二个参数传递给该方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">action</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token class-name static-context">UserController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'profile'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="asset" tabindex="-1"><a class="header-anchor" href="#asset"><span><code v-pre>asset()</code></span></a></h4>
<p><code v-pre>asset</code> 函数使用请求的当前方案（HTTP 或 HTTPS）生成 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">asset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'img/photo.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以通过在<code v-pre>.env</code> 文件中设置 <code v-pre>ASSET_URL</code> 变量来配置资源 URL 主机。如果你将资源托管在外部服务（如 Amazon S3 或其他 CDN）上，这将非常有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// ASSET_URL=http://example.com/assets</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">asset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'img/photo.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// http://example.com/assets/img/photo.jpg</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="route" tabindex="-1"><a class="header-anchor" href="#route"><span><code v-pre>route()</code></span></a></h4>
<p><code v-pre>route</code> 函数为给定的 <a href="https://learnku.com/docs/laravel/11.x/routing#named-routes" target="_blank" rel="noopener noreferrer">命名路由</a>生成 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'route.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果路由接受参数，你可以将它们作为函数的第二个参数传递：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'route.name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下，<code v-pre>route</code> 函数生成一个绝对 URL。如果你希望生成一个相对 URL，你可以将 <code v-pre>false</code> 作为函数的第三个参数传递：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'route.name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="secure-asset" tabindex="-1"><a class="header-anchor" href="#secure-asset"><span><code v-pre>secure_asset()</code></span></a></h4>
<p><code v-pre>secure_asset</code> 函数使用 HTTPS 生成资源的 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">secure_asset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'img/photo.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="secure-url" tabindex="-1"><a class="header-anchor" href="#secure-url"><span><code v-pre>secure_url()</code></span></a></h4>
<p><code v-pre>secure_url</code> 函数生成到给定路径的完全限定 HTTPS URL。额外的 URL 段可以作为函数的第二个参数传递：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">secure_url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/profile'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">secure_url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="to-route" tabindex="-1"><a class="header-anchor" href="#to-route"><span><code v-pre>to_route()</code></span></a></h4>
<p><code v-pre>to_route</code> 函数生成给定 <a href="https://learnku.com/docs/laravel/11.x/routing#named-routes" target="_blank" rel="noopener noreferrer">命名路由</a> 的 <a href="https://learnku.com/docs/laravel/11.x/responses#redirects" target="_blank" rel="noopener noreferrer">重定向 HTTP 响应</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">to_route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users.show'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果需要，你可以将应该分配给重定向的 HTTP 状态码和任何其他响应头作为 <code v-pre>to_route</code> 方法的第三和第四个参数传递：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">to_route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users.show'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">302</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'X-Framework'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="url" tabindex="-1"><a class="header-anchor" href="#url"><span><code v-pre>url()</code></span></a></h4>
<p><code v-pre>url</code> 函数生成到给定路径的完全限定 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/profile'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果没有提供路径，将返回一个 <code v-pre>Illuminate\Routing\UrlGenerator</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$current</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">current</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$full</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">full</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$previous</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">previous</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="杂项-1" tabindex="-1"><a class="header-anchor" href="#杂项-1"><span>杂项</span></a></h2>
<h4 id="abort" tabindex="-1"><a class="header-anchor" href="#abort"><span><code v-pre>abort()</code></span></a></h4>
<p><code v-pre>abort</code> 函数抛出 <a href="https://learnku.com/docs/laravel/11.x/errors#http-exceptions" target="_blank" rel="noopener noreferrer">HTTP 异常</a>，该异常将由 <a href="https://learnku.com/docs/laravel/11.x/errors#the-exception-handler" target="_blank" rel="noopener noreferrer">异常处理器</a>渲染：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">abort</span><span class="token punctuation">(</span><span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你也可以提供异常的消息和应该发送到浏览器的自定义 HTTP 响应头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">abort</span><span class="token punctuation">(</span><span class="token number">403</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Unauthorized.'</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="abort-if" tabindex="-1"><a class="header-anchor" href="#abort-if"><span><code v-pre>abort_if()</code></span></a></h4>
<p>如果给定的布尔表达式评估为 <code v-pre>true</code> 则 <code v-pre>abort_if</code> 函数抛出 HTTP 异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">abort_if</span><span class="token punctuation">(</span><span class="token operator">!</span> <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>和 <code v-pre>abort</code> 方法类似，你也可以将异常的响应文本作为第三个参数提供，并将自定义响应头的数组作为第四个参数传递给函数。</p>
<h4 id="abort-unless" tabindex="-1"><a class="header-anchor" href="#abort-unless"><span><code v-pre>abort_unless()</code></span></a></h4>
<p>如果给定的布尔表达式评估为 <code v-pre>false</code> 则 <code v-pre>abort_unless</code> 函数抛出 HTTP 异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">abort_unless</span><span class="token punctuation">(</span><span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>和 <code v-pre>abort</code> 方法类似，你也可以将异常的响应文本作为第三个参数提供，并将自定义响应头的数组作为第四个参数传递给函数。</p>
<h4 id="app" tabindex="-1"><a class="header-anchor" href="#app"><span><code v-pre>app()</code></span></a></h4>
<p><code v-pre>app</code> 函数返回 <a href="https://learnku.com/docs/laravel/11.x/container" target="_blank" rel="noopener noreferrer">服务容器</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$container</span> <span class="token operator">=</span> <span class="token function">app</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以传递一个类或接口名称来从容器中解析它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$api</span> <span class="token operator">=</span> <span class="token function">app</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'HelpSpot\API'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="auth" tabindex="-1"><a class="header-anchor" href="#auth"><span><code v-pre>auth()</code></span></a></h4>
<p><code v-pre>auth</code> 函数返回一个 <a href="https://learnku.com/docs/laravel/11.x/authentication" target="_blank" rel="noopener noreferrer">认证器</a> 实例。你可以使用它来替代 <code v-pre>Auth</code> facade：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">auth</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果需要，你可以指定你想要访问的哪一个守卫实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">auth</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'admin'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="back" tabindex="-1"><a class="header-anchor" href="#back"><span><code v-pre>back()</code></span></a></h4>
<p><code v-pre>back</code> 函数生成 <a href="https://learnku.com/docs/laravel/11.x/responses#redirects" target="_blank" rel="noopener noreferrer">重定向 HTTP 响应</a> 到用户的上一个位置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">back</span><span class="token punctuation">(</span><span class="token variable">$status</span> <span class="token operator">=</span> <span class="token number">302</span><span class="token punctuation">,</span> <span class="token variable">$headers</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$fallback</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="bcrypt" tabindex="-1"><a class="header-anchor" href="#bcrypt"><span><code v-pre>bcrypt()</code></span></a></h4>
<p><code v-pre>bcrypt</code> 函数使用 Bcrypt <a href="https://learnku.com/docs/laravel/11.x/hashing" target="_blank" rel="noopener noreferrer">哈希</a> 给定的值。你可以使用这个函数替代 <code v-pre>Hash</code> facade ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token function">bcrypt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-secret-password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="blank" tabindex="-1"><a class="header-anchor" href="#blank"><span><code v-pre>blank()</code></span></a></h4>
<p><code v-pre>blank</code> 函数确定给定值是否为「空白」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token string single-quoted-string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'   '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>对于 <code v-pre>blank</code> 的反转，请参阅 <a href="#method-filled"><code v-pre>filled</code></a> 方法。</p>
<h4 id="broadcast" tabindex="-1"><a class="header-anchor" href="#broadcast"><span><code v-pre>broadcast()</code></span></a></h4>
<p><code v-pre>broadcast</code> 函数 <a href="https://learnku.com/docs/laravel/11.x/broadcasting" target="_blank" rel="noopener noreferrer">广播</a> 给定的 <a href="https://learnku.com/docs/laravel/11.x/events" target="_blank" rel="noopener noreferrer">事件</a> 到其它监听器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">broadcast</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">UserRegistered</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">broadcast</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">UserRegistered</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toOthers</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="cache" tabindex="-1"><a class="header-anchor" href="#cache"><span><code v-pre>cache()</code></span></a></h4>
<p><code v-pre>cache</code> 函数可用于从 <a href="https://learnku.com/docs/laravel/11.x/cache" target="_blank" rel="noopener noreferrer">缓存</a> 中获取值。 如果缓存中不存在给定的键，将返回一个可选的默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">cache</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">cache</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过将键 / 值对数组传递给函数来将项目添加到缓存中。 你应该传递缓存值应被视为有效的秒数或持续时间：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">cache</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">cache</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addSeconds</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="class-uses-recursive" tabindex="-1"><a class="header-anchor" href="#class-uses-recursive"><span><code v-pre>class_uses_recursive()</code></span></a></h4>
<p><code v-pre>class_uses_recursive</code> 函数返回一个类使用的所有特征，包括其所有父类使用的特征：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$traits</span> <span class="token operator">=</span> <span class="token function">class_uses_recursive</span><span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="collect" tabindex="-1"><a class="header-anchor" href="#collect"><span><code v-pre>collect()</code></span></a></h4>
<p><code v-pre>collect</code> 函数根据给定值创建一个 <a href="https://learnku.com/docs/laravel/11.x/collections" target="_blank" rel="noopener noreferrer">集合</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'abigail'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="config" tabindex="-1"><a class="header-anchor" href="#config"><span><code v-pre>config()</code></span></a></h4>
<p><code v-pre>config</code> 函数获取 <a href="https://learnku.com/docs/laravel/11.x/configurationn" target="_blank" rel="noopener noreferrer">配置</a> 变量的值。 可以使用「.」语法访问配置值，其中包括文件名和你希望访问的选项。 如果配置选项不存在，可以指定默认值并返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.timezone'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.timezone'</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过传递键 / 值对数组在运行时设置配置变量。 但是请注意，此函数只会影响当前请求的配置值，不会更新你的实际配置值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">config</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'app.debug'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="context" tabindex="-1"><a class="header-anchor" href="#context"><span><code v-pre>context()</code></span></a></h4>
<p><code v-pre>context</code> 函数从 <a href="https://learnku.com/docs/laravel/11.x/context" target="_blank" rel="noopener noreferrer">当前上下文</a> 中获取值。默认值可以指定，并且如果上下文键不存在则返回它</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">context</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'trace_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">context</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'trace_id'</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过传递键 / 值对数组来设置上下文值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">context</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'trace_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">uuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="cookie" tabindex="-1"><a class="header-anchor" href="#cookie"><span><code v-pre>cookie()</code></span></a></h4>
<p><code v-pre>cookie</code> 函数创建一个新的 <a href="https://learnku.com/docs/laravel/11.x/requests#cookies" target="_blank" rel="noopener noreferrer">cookie</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$cookie</span> <span class="token operator">=</span> <span class="token function">cookie</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="csrf-field" tabindex="-1"><a class="header-anchor" href="#csrf-field"><span><code v-pre>csrf_field()</code></span></a></h4>
<p><code v-pre>csrf_field</code> 函数生成一个 HTML <code v-pre>hidden</code> 输入字段，包含 CSRF 令牌的值。例如，使用 <a href="https://learnku.com/docs/laravel/11.x/blade" target="_blank" rel="noopener noreferrer">Blade 语法</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">csrf_field</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="csrf-token" tabindex="-1"><a class="header-anchor" href="#csrf-token"><span><code v-pre>csrf_token()</code></span></a></h4>
<p><code v-pre>csrf_token</code> 函数检索当前 CSRF 令牌的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$token</span> <span class="token operator">=</span> <span class="token function">csrf_token</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="decrypt" tabindex="-1"><a class="header-anchor" href="#decrypt"><span><code v-pre>decrypt()</code></span></a></h4>
<p><code v-pre>decrypt</code> 函数 <a href="https://learnku.com/docs/laravel/11.x/encryption" target="_blank" rel="noopener noreferrer">解密</a> 给定值。你可以使用这个函数替代 <code v-pre>Crypt</code> facade：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token function">decrypt</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="dd" tabindex="-1"><a class="header-anchor" href="#dd"><span><code v-pre>dd()</code></span></a></h4>
<p><code v-pre>dd</code> 函数转储给定变量并结束脚本的执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">dd</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">dd</span><span class="token punctuation">(</span><span class="token variable">$value1</span><span class="token punctuation">,</span> <span class="token variable">$value2</span><span class="token punctuation">,</span> <span class="token variable">$value3</span><span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你不想在转储变量后中止脚本的执行，请使用 <a href="#method-dump"><code v-pre>dump</code></a> 函数。</p>
<h4 id="dispatch" tabindex="-1"><a class="header-anchor" href="#dispatch"><span><code v-pre>dispatch()</code></span></a></h4>
<p><code v-pre>dispatch</code> 函数将给定的 <a href="https://learnku.com/docs/laravel/11.x/queues#creating-jobss" target="_blank" rel="noopener noreferrer">任务</a> 推送到 Laravel <a href="https://learnku.com/docs/laravel/11.x/queues" target="_blank" rel="noopener noreferrer">任务队列</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">App<span class="token punctuation">\</span>Jobs<span class="token punctuation">\</span>SendEmails</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="dispatch-sync" tabindex="-1"><a class="header-anchor" href="#dispatch-sync"><span><code v-pre>dispatch_sync()</code></span></a></h4>
<p><code v-pre>dispatch_sync</code> 函数将给定的任务推送到 <a href="https://learnku.com/docs/laravel/11.x/queues#synchronous-dispatching" target="_blank" rel="noopener noreferrer">同步</a> 队列，以便立即处理：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">dispatch_sync</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">App<span class="token punctuation">\</span>Jobs<span class="token punctuation">\</span>SendEmails</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="dump" tabindex="-1"><a class="header-anchor" href="#dump"><span><code v-pre>dump()</code></span></a></h4>
<p><code v-pre>dump</code> 函数转储给定变量：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">dump</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">dump</span><span class="token punctuation">(</span><span class="token variable">$value1</span><span class="token punctuation">,</span> <span class="token variable">$value2</span><span class="token punctuation">,</span> <span class="token variable">$value3</span><span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想在转储变量后停止执行脚本，请使用 <a href="#method-dd"><code v-pre>dd</code></a> 函数。</p>
<h4 id="encrypt" tabindex="-1"><a class="header-anchor" href="#encrypt"><span><code v-pre>encrypt()</code></span></a></h4>
<p><code v-pre>encrypt</code> 函数 <a href="https://learnku.com/docs/laravel/11.x/encryption" target="_blank" rel="noopener noreferrer">加密</a> 给定值。你可以使用这个函数替代 <code v-pre>Crypt</code> facade ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$secret</span> <span class="token operator">=</span> <span class="token function">encrypt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-secret-value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="env" tabindex="-1"><a class="header-anchor" href="#env"><span><code v-pre>env()</code></span></a></h4>
<p><code v-pre>env</code> 函数检索一个 <a href="https://learnku.com/docs/laravel/11.x/configuration#environment-configuration" target="_blank" rel="noopener noreferrer">环境变量</a> 的值或返回一个默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$env</span> <span class="token operator">=</span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'APP_ENV'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$env</span> <span class="token operator">=</span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'APP_ENV'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'production'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
如果你在部署过程中执行了 <code v-pre>config:cache</code> 命令，请确保你只在配置文件中调用 <code v-pre>env</code> 函数。一旦配置被缓存，<code v-pre>.env</code> 文件不会被加载，并且所有调用 <code v-pre>env</code> 函数的地方都会返回 <code v-pre>null</code>。</p>
</blockquote>
<h4 id="event" tabindex="-1"><a class="header-anchor" href="#event"><span><code v-pre>event()</code></span></a></h4>
<p><code v-pre>event</code> 函数将给定的 <a href="https://learnku.com/docs/laravel/11.x/events" target="_blank" rel="noopener noreferrer">事件</a> 分发到它的监听器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">event</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">UserRegistered</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="fake" tabindex="-1"><a class="header-anchor" href="#fake"><span><code v-pre>fake()</code></span></a></h4>
<p><code v-pre>fake</code> 函数从容器中解析一个 <a href="https://github.com/FakerPHP/Faker" target="_blank" rel="noopener noreferrer">Faker</a> 单例，这在创建模型工厂、数据库种子数据、测试以及原型视图中的假数据时非常有用：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@for($i = 0; $i &lt; 10; $i++)</span>
<span class="line">    &lt;dl&gt;</span>
<span class="line">        &lt;dt&gt;Name&lt;/dt&gt;</span>
<span class="line">        &lt;dd&gt;{{ fake()-&gt;name() }}&lt;/dd&gt;</span>
<span class="line"></span>
<span class="line">        &lt;dt&gt;Email&lt;/dt&gt;</span>
<span class="line">        &lt;dd&gt;{{ fake()-&gt;unique()-&gt;safeEmail() }}&lt;/dd&gt;</span>
<span class="line">    &lt;/dl&gt;</span>
<span class="line">@endfor</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认情况下，<code v-pre>fake</code> 函数将使用 <code v-pre>config/app.php</code> 配置中的 <code v-pre>app.faker_locale</code> 配置选项。通常，这个配置项是通过 <code v-pre>APP_FAKER_LOCALE</code> 环境变量设置的。你也可以通过将语言环境传递给 <code v-pre>fake</code> 函数来指定语言环境。每个 语言环境都会解析一个单独的单例</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">fake</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'nl_NL'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="filled" tabindex="-1"><a class="header-anchor" href="#filled"><span><code v-pre>filled()</code></span></a></h4>
<p><code v-pre>filled</code> 函数用于判断给定值是否不是「空白」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token string single-quoted-string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'   '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>对于 <code v-pre>filled</code> 的反转，请参阅 <a href="#method-blank"><code v-pre>blank</code></a> 方法。</p>
<h4 id="info" tabindex="-1"><a class="header-anchor" href="#info"><span><code v-pre>info()</code></span></a></h4>
<p><code v-pre>info</code> 函数将信息写入应用程序的 <a href="https://learnku.com/docs/laravel/11.x/logging" target="_blank" rel="noopener noreferrer">日志</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">info</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Some helpful information!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>上下文数据数组也可以传递给函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">info</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'User login attempt failed.'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="literal" tabindex="-1"><a class="header-anchor" href="#literal"><span><code v-pre>literal()</code></span></a></h4>
<p><code v-pre>literal</code> 函数使用给定的命名参数作为属性创建一个新的 <a href="https://www.php.net/manual/en/class.stdclass.php" target="_blank" rel="noopener noreferrer">stdClass</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$obj</span> <span class="token operator">=</span> <span class="token function">literal</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">name</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Joe'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">languages</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'PHP'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Ruby'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$obj</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">;</span> <span class="token comment">// 'Joe'</span></span>
<span class="line"><span class="token variable">$obj</span><span class="token operator">-></span><span class="token property">languages</span><span class="token punctuation">;</span> <span class="token comment">// ['PHP', 'Ruby']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="logger" tabindex="-1"><a class="header-anchor" href="#logger"><span><code v-pre>logger()</code></span></a></h4>
<p><code v-pre>logger</code> 函数可用于向 <a href="https://learnku.com/docs/laravel/11.x/logging" target="_blank" rel="noopener noreferrer">日志</a> 写入 <code v-pre>debug</code> 级别的信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">logger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Debug message'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>上下文数据数组也可以传递给函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">logger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'User has logged in.'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果没有传递值给函数，将返回 <a href="https://learnku.com/docs/laravel/11.x/errors#logging" target="_blank" rel="noopener noreferrer">logger</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">logger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'You are not allowed here.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="method-field" tabindex="-1"><a class="header-anchor" href="#method-field"><span><code v-pre>method_field()</code></span></a></h4>
<p><code v-pre>method_field</code> 函数生成一个 HTML <code v-pre>hidden</code> 输入字段，其中包含表单 HTTP 动词的伪造值。例如，使用 <a href="https://learnku.com/docs/laravel/11.x/blade" target="_blank" rel="noopener noreferrer">Blade 语法</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>form method<span class="token operator">=</span><span class="token string double-quoted-string">"POST"</span><span class="token operator">></span></span>
<span class="line">    <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">method_field</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'DELETE'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>form<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="now" tabindex="-1"><a class="header-anchor" href="#now"><span><code v-pre>now()</code></span></a></h4>
<p><code v-pre>now</code> 函数为当前时间创建一个新的 <code v-pre>Illuminate\Support\Carbon</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$now</span> <span class="token operator">=</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="old" tabindex="-1"><a class="header-anchor" href="#old"><span><code v-pre>old()</code></span></a></h4>
<p><code v-pre>old</code> 函数 <a href="https://learnku.com/docs/laravel/11.x/requests#retrieving-input" target="_blank" rel="noopener noreferrer">检索</a> 被闪存到 session 中的 <a href="https://learnku.com/docs/laravel/11.x/requests#old-input" target="_blank" rel="noopener noreferrer">旧输入值</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>由于 <code v-pre>old</code> 函数的第二个参数提供的「默认值」通常是 Eloquent 模型的属性，Laravel 允许你简单地将整个 Eloquent 模型作为 <code v-pre>old</code> 函数的第二个参数传递。这样做时，Laravel 将假定提供给 <code v-pre>old</code> 函数的第一个参数是 Eloquent 属性的名称，该属性应被视为「默认值」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 等价于...</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="once" tabindex="-1"><a class="header-anchor" href="#once"><span><code v-pre>once()</code></span></a></h4>
<p><code v-pre>once</code> 函数执行给定回调并在请求期间将结果缓存在内存中。任何后续使用相同回调的 <code v-pre>once</code> 函数的调用将返回之前缓存的结果：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">function</span> <span class="token function-definition function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">int</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">once</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">random_int</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 123</span></span>
<span class="line"><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 123 (缓存的结果)</span></span>
<span class="line"><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 123 (缓存的结果)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当 <code v-pre>once</code> 函数在对象实例内部执行时，缓存结果将唯一于该对象实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">NumberService</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">once</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$service</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NumberService</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$service</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$service</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// (缓存的结果)</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$secondService</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NumberService</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$secondService</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$secondService</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// (缓存的结果)</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="optional" tabindex="-1"><a class="header-anchor" href="#optional"><span><code v-pre>optional()</code></span></a></h4>
<p><code v-pre>optional</code> 函数接受任意参数并允许你访问该对象上的属性或调用方法。如果给定的对象是 <code v-pre>null</code>，属性和方法将返回 <code v-pre>null</code> 而非引起错误：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">optional</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">address</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">street</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">{</span><span class="token operator">!</span><span class="token operator">!</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token function">optional</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">)</span> <span class="token operator">!</span><span class="token operator">!</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>optional</code> 函数还接受闭包作为其第二个参数。如果第一个参数提供的值不是 null，则将调用闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">optional</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="policy" tabindex="-1"><a class="header-anchor" href="#policy"><span><code v-pre>policy()</code></span></a></h4>
<p><code v-pre>policy</code> 方法检索给定类的 <a href="https://learnku.com/docs/laravel/11.x/authorization#creating-policies" target="_blank" rel="noopener noreferrer">policy</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$policy</span> <span class="token operator">=</span> <span class="token function">policy</span><span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="redirect" tabindex="-1"><a class="header-anchor" href="#redirect"><span><code v-pre>redirect()</code></span></a></h4>
<p><code v-pre>redirect</code> 函数返回 <a href="https://learnku.com/docs/laravel/11.x/responses#redirects" target="_blank" rel="noopener noreferrer">重定向 HTTP 响应</a>，如果没有参数调用它，则返回重定向器实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token variable">$to</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">,</span> <span class="token variable">$status</span> <span class="token operator">=</span> <span class="token number">302</span><span class="token punctuation">,</span> <span class="token variable">$headers</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$https</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'route.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="report" tabindex="-1"><a class="header-anchor" href="#report"><span><code v-pre>report()</code></span></a></h4>
<p><code v-pre>report</code> 函数将使用你的 <a href="https://learnku.com/docs/laravel/11.x/errors#the-exception-handler" target="_blank" rel="noopener noreferrer">异常处理程序</a> 报告异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">report</span><span class="token punctuation">(</span><span class="token variable">$e</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>report</code> 函数也接受字符串作为参数。当给函数传递字符串时，该函数将创建一个以给定字符串为消息的异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">report</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Something went wrong.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="report-if" tabindex="-1"><a class="header-anchor" href="#report-if"><span><code v-pre>report_if()</code></span></a></h4>
<p>如果给定条件为 <code v-pre>true</code>，<code v-pre>report_if</code> 函数将使用你的 <a href="https://learnku.com/docs/laravel/11.x/errors#the-exception-handler" target="_blank" rel="noopener noreferrer">异常处理程序</a> 报告异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">report_if</span><span class="token punctuation">(</span><span class="token variable">$shouldReport</span><span class="token punctuation">,</span> <span class="token variable">$e</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">report_if</span><span class="token punctuation">(</span><span class="token variable">$shouldReport</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Something went wrong.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="report-unless" tabindex="-1"><a class="header-anchor" href="#report-unless"><span><code v-pre>report_unless()</code></span></a></h4>
<p>如果给定条件为 <code v-pre>false</code>，<code v-pre>report_unless</code> 函数将使用你的 <a href="https://learnku.com/docs/laravel/11.x/errors#the-exception-handler" target="_blank" rel="noopener noreferrer">异常处理程序</a> 报告异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">report_unless</span><span class="token punctuation">(</span><span class="token variable">$reportingDisabled</span><span class="token punctuation">,</span> <span class="token variable">$e</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">report_unless</span><span class="token punctuation">(</span><span class="token variable">$reportingDisabled</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Something went wrong.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="request" tabindex="-1"><a class="header-anchor" href="#request"><span><code v-pre>request()</code></span></a></h4>
<p><code v-pre>request</code> 函数返回当前 <a href="https://learnku.com/docs/laravel/11.x/requests" target="_blank" rel="noopener noreferrer">请求</a> 实例或从当前请求获取输入字段的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span> <span class="token operator">=</span> <span class="token function">request</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">request</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="rescue" tabindex="-1"><a class="header-anchor" href="#rescue"><span><code v-pre>rescue()</code></span></a></h4>
<p><code v-pre>rescue</code> 函数执行给定闭包并捕获执行期间发生的任何异常。所有捕获的异常都将发送到 <a href="https://learnku.com/docs/laravel/11.x/errors#the-exception-handler" target="_blank" rel="noopener noreferrer">异常处理程序</a>；但是，请求将继续处理：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">rescue</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以向 <code v-pre>rescue</code> 函数传递第二个参数。此参数将是在执行闭包时发生异常应返回的「默认」值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">rescue</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">rescue</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">failure</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可以提供 <code v-pre>report</code> 参数给 <code v-pre>rescue</code> 函数来决定是否通过 <code v-pre>report</code> 函数报告异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">rescue</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token argument-name">report</span><span class="token punctuation">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Throwable</span> <span class="token variable">$throwable</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$throwable</span> <span class="token keyword">instanceof</span> <span class="token class-name">InvalidArgumentException</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="resolve" tabindex="-1"><a class="header-anchor" href="#resolve"><span><code v-pre>resolve()</code></span></a></h4>
<p><code v-pre>resolve</code> 函数使用 <a href="https://learnku.com/docs/laravel/11.x/container" target="_blank" rel="noopener noreferrer">服务容器</a> 将给定的类或接口名解析为实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$api</span> <span class="token operator">=</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'HelpSpot\API'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="response" tabindex="-1"><a class="header-anchor" href="#response"><span><code v-pre>response()</code></span></a></h4>
<p><code v-pre>response</code> 函数创建一个 <a href="https://learnku.com/docs/laravel/11.x/responses" target="_blank" rel="noopener noreferrer">响应</a> 实例或获取响应工厂的实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="retry" tabindex="-1"><a class="header-anchor" href="#retry"><span><code v-pre>retry()</code></span></a></h4>
<p><code v-pre>retry</code> 函数尝试执行给定的回调，直到达到给定的最大尝试阈值。如果回调没有抛出异常，将返回它的返回值。如果回调抛出异常，它将自动被重试。如果超过最大尝试次数，将抛出异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 尝试 5 次，每次尝试之间休息 100ms...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你希望手动计算尝试之间睡眠的毫秒数，你可以将闭包作为第三个参数传递给 <code v-pre>retry</code> 函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Exception</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$attempt</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Exception</span> <span class="token variable">$exception</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$attempt</span> <span class="token operator">*</span> <span class="token number">100</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>方便起见，你可以将数组作为 <code v-pre>retry</code> 函数的第一个参数提供。该数组将用于确定后续尝试之间的毫秒数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">retry</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//  第一次重试睡眠 100ms，第二次重试睡眠 200ms...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要仅在特定条件下重试，你可以将闭包作为 <code v-pre>retry</code> 函数的第四个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Exception</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Exception</span> <span class="token variable">$exception</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$exception</span> <span class="token keyword">instanceof</span> <span class="token class-name">RetryException</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="session" tabindex="-1"><a class="header-anchor" href="#session"><span><code v-pre>session()</code></span></a></h4>
<p><code v-pre>session</code> 函数可用于获取或设置 <a href="https://learnku.com/docs/laravel/11.x/sessio" target="_blank" rel="noopener noreferrer">session</a> 值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">session</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以通过向函数传递键 / 值对数组来设置值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'chairs'</span> <span class="token operator">=></span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'instruments'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果没有传递值给函数，会话存储将被返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="tap" tabindex="-1"><a class="header-anchor" href="#tap"><span><code v-pre>tap()</code></span></a></h4>
<p><code v-pre>tap</code> 函数接受两个参数：任意的 <code v-pre>$value</code> 和闭包。<code v-pre>$value</code> 将被传递给闭包，然后由 <code v-pre>tap</code> 函数返回。 闭包的返回值是无关紧要的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">tap</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'taylor'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果没有将闭包传递给 <code v-pre>tap</code> 函数，你可以调用给定 <code v-pre>$value</code> 上的任何方法。你调用的方法的返回值始终是 <code v-pre>$value</code>，不管这个方法在定义中实际返回什么。例如，Eloquent 的 <code v-pre>update</code> 方法通常返回一个整数。然而，我们可以通过 <code v-pre>tap</code> 函数链接 <code v-pre>update</code> 方法调用，强制该方法返回模型本身：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">tap</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token variable">$name</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token variable">$email</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要向类添加 <code v-pre>tap</code> 方法，你可以为类添加 <code v-pre>Illuminate\Support\Traits\Tappable</code> trait。这个 trait 的 <code v-pre>tap</code> 方法接受闭包作为其唯一的参数。对象实例本身将被传递给闭包，然后通过 <code v-pre>tap</code> 方法返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">tap</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="throw-if" tabindex="-1"><a class="header-anchor" href="#throw-if"><span><code v-pre>throw_if()</code></span></a></h4>
<p>如果给定的布尔表达式评估为 <code v-pre>true</code>，则 <code v-pre>throw_if</code> 函数抛出给定的异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">throw_if</span><span class="token punctuation">(</span><span class="token operator">!</span> <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name static-context">AuthorizationException</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">throw_if</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token operator">!</span> <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token class-name static-context">AuthorizationException</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'你不允许访问此页面。'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="throw-unless" tabindex="-1"><a class="header-anchor" href="#throw-unless"><span><code v-pre>throw_unless()</code></span></a></h4>
<p>如果给定的布尔表达式评估为 <code v-pre>false</code>，则 <code v-pre>throw_unless</code> 函数抛出给定的异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">throw_unless</span><span class="token punctuation">(</span><span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name static-context">AuthorizationException</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">throw_unless</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token class-name static-context">AuthorizationException</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'你不允许访问此页面。'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="today" tabindex="-1"><a class="header-anchor" href="#today"><span><code v-pre>today()</code></span></a></h4>
<p><code v-pre>today</code> 函数为当前日期创建一个新的 <code v-pre>Illuminate\Support\Carbon</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$today</span> <span class="token operator">=</span> <span class="token function">today</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="trait-uses-recursive" tabindex="-1"><a class="header-anchor" href="#trait-uses-recursive"><span><code v-pre>trait_uses_recursive()</code></span></a></h4>
<p><code v-pre>trait_uses_recursive</code> 函数返回特征使用的所有 trait：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$traits</span> <span class="token operator">=</span> <span class="token function">trait_uses_recursive</span><span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="transform" tabindex="-1"><a class="header-anchor" href="#transform"><span><code v-pre>transform()</code></span></a></h4>
<p><code v-pre>transform</code> 函数在给定值不是 <a href="#method-blank">blank</a> 的情况下对其执行闭包，然后返回闭包的返回值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$callback</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">transform</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 10</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>第三个参数可以传递默认值或闭包给函数。如果给定值是 空，将返回该值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">transform</span><span class="token punctuation">(</span><span class="token constant">null</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'The value is blank'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// The value is blank</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="validator" tabindex="-1"><a class="header-anchor" href="#validator"><span><code v-pre>validator()</code></span></a></h4>
<p><code v-pre>validator</code> 函数使用给定的参数创建一个新的 <a href="http://doc.miaoft.cn/docs/11/basics/validation" target="_blank" rel="noopener noreferrer">validator</a> 实例。你可以用它替代 <code v-pre>Validator</code> facade：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token function">validator</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">,</span> <span class="token variable">$messages</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="value" tabindex="-1"><a class="header-anchor" href="#value"><span><code v-pre>value()</code></span></a></h4>
<p><code v-pre>value</code> 函数返回它所接收到的值。但是，如果你传递一个闭包给该函数，闭包将被执行，并且其返回的值将被返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">value</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">value</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可以向 <code v-pre>value</code> 函数传递额外的参数。如果第一个参数是闭包，则额外的参数将作为参数传递给闭包，否则它们将被忽略：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">value</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$name</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$name</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Taylor'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="view" tabindex="-1"><a class="header-anchor" href="#view"><span><code v-pre>view()</code></span></a></h4>
<p><code v-pre>view</code> 函数检索一个 <a href="https://learnku.com/docs/laravel/11.x/views" target="_blank" rel="noopener noreferrer">视图</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="with" tabindex="-1"><a class="header-anchor" href="#with"><span><code v-pre>with()</code></span></a></h4>
<p>with 函数返回它被赋予的值。如果闭包作为该函数的第二个参数传递，则会执行闭包并返回其返回值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$callback</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">mixed</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">is_numeric</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token variable">$value</span> <span class="token operator">*</span> <span class="token number">2</span> <span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">with</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 10</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">with</span><span class="token punctuation">(</span><span class="token constant">null</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 0</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">with</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 5</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="其他实用工具" tabindex="-1"><a class="header-anchor" href="#其他实用工具"><span>其他实用工具</span></a></h2>
<h3 id="基准测试" tabindex="-1"><a class="header-anchor" href="#基准测试"><span>基准测试</span></a></h3>
<p>有时你可能希望快速测试应用程序的某些部分的性能。在这些场合，你可以使用 <code v-pre>Benchmark</code> 支持类来测量完成给定回调所需的毫秒数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Benchmark</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Benchmark</span><span class="token operator">::</span><span class="token function">dd</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 0.1 ms</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Benchmark</span><span class="token operator">::</span><span class="token function">dd</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'Scenario 1'</span> <span class="token operator">=></span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// 0.5 ms</span></span>
<span class="line">    <span class="token string single-quoted-string">'Scenario 2'</span> <span class="token operator">=></span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// 20.0 ms</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认情况下，给定的回调将被执行一次（一次迭代），其持续时间将在浏览器 / 控制台中显示。</p>
<p>要多次调用回调，你可以指定回调应该被调用的迭代次数作为方法的第二个参数。当多次执行回调时，<code v-pre>Benchmark</code> 类将返回执行回调所需的平均毫秒数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Benchmark</span><span class="token operator">::</span><span class="token function">dd</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token argument-name">iterations</span><span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 0.5 ms</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>有时，你可能想要在测试回调的执行时仍然获得回调返回的值。<code v-pre>value</code> 方法将返回一个包含回调返回值和执行回调所花费毫秒数的元组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span><span class="token variable">$count</span><span class="token punctuation">,</span> <span class="token variable">$duration</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name static-context">Benchmark</span><span class="token operator">::</span><span class="token function">value</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="日期" tabindex="-1"><a class="header-anchor" href="#日期"><span>日期</span></a></h3>
<p>Laravel 包含 <a href="https://carbon.nesbot.com/docs/" target="_blank" rel="noopener noreferrer">Carbon</a>，一个强大的日期和时间操作库。要创建一个新的 <code v-pre>Carbon</code> 实例，你可以调用 <code v-pre>now</code> 函数。此函数在 Laravel 应用程序中全局可用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$now</span> <span class="token operator">=</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者，你可以使用 <code v-pre>Illuminate\Support\Carbon</code> 类来创建一个新的 <code v-pre>Carbon</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Carbon</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$now</span> <span class="token operator">=</span> <span class="token class-name static-context">Carbon</span><span class="token operator">::</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>有关 Carbon 及其功能的详尽讨论，请查阅 <a href="https://carbon.nesbot.com/docs/" target="_blank" rel="noopener noreferrer">Carbon 官方文档</a>。</p>
<h3 id="抽奖" tabindex="-1"><a class="header-anchor" href="#抽奖"><span>抽奖</span></a></h3>
<p>Laravel 的 lottery 类可以根据一组给定的赔率执行回调。当你只想为你的传入请求的一部分执行代码时，这特别有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Lottery</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Lottery</span><span class="token operator">::</span><span class="token function">odds</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">winner</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">won</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">loser</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">lost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">choose</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以将 Laravel 的抽奖类与其他 Laravel 功能结合使用。例如，你可能希望只向你的异常处理程序报告慢查询的一小部分。由于抽奖类是可调用的，我们可以将该类的实例传递给任何接受可调用对象的方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Carbon<span class="token punctuation">\</span>CarbonInterval</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>DB</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Lottery</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">whenQueryingForLongerThan</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token class-name static-context">CarbonInterval</span><span class="token operator">::</span><span class="token function">seconds</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token class-name static-context">Lottery</span><span class="token operator">::</span><span class="token function">odds</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">winner</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">report</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Querying > 2 seconds.'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="测试抽奖" tabindex="-1"><a class="header-anchor" href="#测试抽奖"><span>测试抽奖</span></a></h4>
<p>Laravel 提供了一些简单的方法，让你可以轻松测试应用程序的抽奖调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 抽奖将总是赢...</span></span>
<span class="line"><span class="token class-name static-context">Lottery</span><span class="token operator">::</span><span class="token function">alwaysWin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 抽奖将总是输...</span></span>
<span class="line"><span class="token class-name static-context">Lottery</span><span class="token operator">::</span><span class="token function">alwaysLose</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 抽奖将先赢然后输，最后恢复正常行为...</span></span>
<span class="line"><span class="token class-name static-context">Lottery</span><span class="token operator">::</span><span class="token function">fix</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token constant boolean">true</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 抽奖将恢复正常行为...</span></span>
<span class="line"><span class="token class-name static-context">Lottery</span><span class="token operator">::</span><span class="token function">determineResultsNormally</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="管道" tabindex="-1"><a class="header-anchor" href="#管道"><span>管道</span></a></h3>
<p>Laravel 的 <code v-pre>Pipeline</code> facade 提供了一种便捷的方式来通过一系列可调用类、闭包或可调用对象「管道化」给定输入，让每个类都有机会检查或修改输入并调用管道中的下一个可调用对象：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Closure</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Pipeline</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Pipeline</span><span class="token operator">::</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">through</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">                <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Closure</span> <span class="token variable">$next</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">                    <span class="token keyword">return</span> <span class="token variable">$next</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Closure</span> <span class="token variable">$next</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">                    <span class="token keyword">return</span> <span class="token variable">$next</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如你所见，管道中的每个可调用类或闭包都被赋予输入和一个 <code v-pre>$next</code> 闭包。调用 <code v-pre>$next</code> 闭包将调用管道中的下一个可调用对象。你可能已经注意到，这与 <a href="https://learnku.com/docs/laravel/11.x/middleware" target="_blank" rel="noopener noreferrer">中间件</a> 非常相似。</p>
<p>当管道中的最后一个可调用对象调用 <code v-pre>$next</code> 闭包时，<code v-pre>then</code> 方法提供的可调用对象将被调用。通常，这个可调用对象只是简单地返回给定输入。</p>
<p>当然，正如之前讨论的，你不仅限于为你的管道提供闭包。你还可以提供可调用的类。如果提供了一个类名，该类将通过 Laravel 的<a href="https://learnku.com/docs/laravel/11.x/container" target="_blank" rel="noopener noreferrer">服务容器</a>实例化，允许将依赖项注入到可调用类中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Pipeline</span><span class="token operator">::</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">through</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">                <span class="token class-name static-context">GenerateProfilePhoto</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token class-name static-context">ActivateSubscription</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token class-name static-context">SendWelcomeEmail</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="睡眠" tabindex="-1"><a class="header-anchor" href="#睡眠"><span>睡眠</span></a></h3>
<p>Laravel 的 <code v-pre>Sleep</code> 类是围绕 PHP 原生 <code v-pre>sleep</code> 和 <code v-pre>usleep</code> 函数的轻量级封装，提供更高的可测试性，同时也为处理时间提供了一个对开发者友好的 API：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Sleep</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$waiting</span> <span class="token operator">=</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token variable">$waiting</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">second</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$waiting</span> <span class="token operator">=</span> <span class="token comment">/* ... */</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>Sleep</code> 类提供了各种方法，允许你使用不同的时间单位：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 暂停执行 90 秒...</span></span>
<span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token number">1.5</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">minutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 暂停执行 2 秒...</span></span>
<span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">seconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 暂停执行 500 毫秒...</span></span>
<span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">milliseconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 暂停执行 5,000 微秒...</span></span>
<span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token number">5000</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">microseconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 暂停执行到给定的时间...</span></span>
<span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">until</span><span class="token punctuation">(</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinute</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// PHP 原生「sleep」函数的别名...</span></span>
<span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// PHP 原生「usleep」函数的别名...</span></span>
<span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">usleep</span><span class="token punctuation">(</span><span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>为了方便组合时间单位，你可以使用 <code v-pre>and</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">second</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">and</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">milliseconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="测试睡眠" tabindex="-1"><a class="header-anchor" href="#测试睡眠"><span>测试睡眠</span></a></h4>
<p>当在测试中利用 <code v-pre>Sleep</code> 类或 PHP 原生的 sleep 函数时，你的测试将暂停执行。可以想象，这会使你的测试套件显著变慢。例如，假设你正在测试以下代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$waiting</span> <span class="token operator">=</span> <span class="token comment">/* ... */</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$seconds</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token variable">$waiting</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token variable">$seconds</span><span class="token operator">++</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">seconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$waiting</span> <span class="token operator">=</span> <span class="token comment">/* ... */</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通常，测试这段代码至少会花费一秒钟。幸运的是，<code v-pre>Sleep</code> 类允许我们「伪造」睡眠，使我们的测试套件保持快速：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">//Pest示例</span></span>
<span class="line"><span class="token function">it</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'waits until ready'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">//PHPUnit示例</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_it_waits_until_ready</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当伪造了 <code v-pre>Sleep</code> 类时，实际的执行暂停被绕过，导致测试显著加快。</p>
<p>一旦 <code v-pre>Sleep</code> 类被伪造，就可以对在应用程序代码中应该发生的预期「睡眠」进行断言。为了说明这一点，让我们假设我们正在测试的代码会三次暂停执行，每次暂停的时间都会增加一秒。使用 <code v-pre>assertSequence</code> 方法，我们可以断言在保持测试速度的同时代码「睡眠」了适当时间：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">//Pest示例</span></span>
<span class="line"><span class="token function">it</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'checks if ready three times'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">assertSequence</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">second</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">seconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">seconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">//PHPUnit示例</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_it_checks_if_ready_four_times</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">assertSequence</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">second</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">seconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">seconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，<code v-pre>Sleep</code> 类提供了许多其他的断言，你可以在测试时使用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Carbon<span class="token punctuation">\</span>CarbonInterval</span> <span class="token keyword">as</span> Duration<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Sleep</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 断言调用 sleep 函数 3 次...</span></span>
<span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">assertSleptTimes</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 针对睡眠持续时间的断言...</span></span>
<span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">assertSlept</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Duration</span> <span class="token variable">$duration</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">bool</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token comment">/* ... */</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token argument-name">times</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 断言 Sleep 类从未被调用...</span></span>
<span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">assertNeverSlept</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 断言，即使 Sleep 被调用，也不会发生执行暂停...</span></span>
<span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">assertInsomniac</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>有时在应用程序代码中伪造睡眠发生时执行某个操作可能会很有用。要做到这一点，你可以提供一个回调给 <code v-pre>whenFakingSleep</code> 方法。在下面的例子中，我们使用 Laravel 的<a href="https://learnku.com/docs/laravel/11.x/mocking#interacting-with-time" target="_blank" rel="noopener noreferrer">时间操作帮助函数</a>来即时进入每次睡眠的时间：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Carbon<span class="token punctuation">\</span>CarbonInterval</span> <span class="token keyword">as</span> Duration<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">freezeTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">whenFakingSleep</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Duration</span> <span class="token variable">$duration</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 进行时间进展，当伪造睡眠时...</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">travel</span><span class="token punctuation">(</span><span class="token variable">$duration</span><span class="token operator">-></span><span class="token property">totalMilliseconds</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">milliseconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>因为进入时间是一个常见需求，<code v-pre>fake</code> 方法接受 <code v-pre>syncWithCarbon</code> 参数，在睡眠测试中保持 Carbon 同步：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token argument-name">syncWithCarbon</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$start</span> <span class="token operator">=</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Sleep</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">second</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$start</span><span class="token operator">-></span><span class="token function">diffForHumans</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1 秒前</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Laravel 在其内部使用 <code v-pre>Sleep</code> 类，每当要暂停执行时。例如，<a href="#method-retry"><code v-pre>retry</code></a> 帮助器使用 <code v-pre>Sleep</code> 类进行睡眠，从而提高在使用该帮助器时的可测试性。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/helpersmd/16679" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/he...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/helpersmd/16679" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/he...</a></p>
</blockquote>
</div></template>


